package com.Managers;


import android.app.Activity;
import android.support.v4.view.PagerTabStrip;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.FrameLayout;
import com.Adapters.TabsPagerAdapter;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.shareup.R;
import com.utils.PagerUtil;


public class HomeManagerClass {

    public static FrameLayout f_explore;

    public static boolean isMineAdded = true;

    private Activity context;

    private FrameLayout f_follow;

    private FrameLayout f_mine;

    private TabsPagerAdapter pagerAdapter;

    private PagerTabStrip pagerTab;

    private ViewPager vpager;

    public HomeManagerClass(ViewPager vpager,
                TabsPagerAdapter pagerAdapter, Activity context,
                PagerTabStrip pagerTab) {

        super();

        this.vpager = vpager;
        this.pagerAdapter = pagerAdapter;
        this.context = context;
        this.pagerTab = pagerTab;
    }

    public void changeViewMenu(String TAG) {

        if (f_explore == null && f_mine == null) {
            f_explore = (FrameLayout) context.getLayoutInflater().inflate(R.layout.fragment_explore, null);
            f_explore.setTag(PagerUtil.FRAGMENT_NEWS);
            f_mine = (FrameLayout) context.getLayoutInflater().inflate(R.layout.fragment_mine, null);
            f_mine.setTag(PagerUtil.FRAGMENT_MINE);
            f_follow = (FrameLayout) context.getLayoutInflater().inflate(R.layout.fragment_followers, null);
            f_follow.setTag(PagerUtil.FRAGMENT_FOLLOWERS);
            pagerAdapter.addView(f_explore);
            pagerAdapter.addView(f_mine);
            pagerAdapter.notifyDataSetChanged();
            FragmentManagerClass.aExplore.clear();

        }
        else {
            String currentTag = "";
            if (pagerAdapter.getCount() > 0)
                currentTag = pagerAdapter.getView(0).getTag().toString();

            // IF WE ARE FROM ANY TAB TO LOAD FOLLOWERS
            if (TAG.equals(PagerUtil.FRAGMENT_FOLLOWERS)) {
                pagerAdapter.removeView(vpager, f_explore);
                pagerAdapter.addView(f_follow);

            }
            // IF WE ARE FROM FOLLOWERS TO ANY OTHER MENU
            // DELETE FOLLOWERS VIEW AND ADD EXPLORE ONE
            else
                if (currentTag.equals(PagerUtil.FRAGMENT_FOLLOWERS)) {
                    pagerAdapter.removeView(vpager, f_follow);
                    pagerAdapter.addView(f_explore);

                }

            // IF WE DONT WANT TO LOAD FOLLOWERS
            // JUS CLEAR THE EXPLORE VIEW FOR EACH MENU

            if (!TAG.equals(PagerUtil.FRAGMENT_FOLLOWERS)) {
                f_explore.setTag(TAG);
                FragmentManagerClass.aExplore.clear();
            }
            // AND IF WE WANT TO GO TO THE NEWS MENU, WE SHOULD ADD THE MINE TAB
            if (TAG.equals(PagerUtil.FRAGMENT_NEWS)) {
                if (!isMineAdded) {
                    pagerAdapter.addView(f_mine);
                    isMineAdded = true;
                }

            }
            else
                if (isMineAdded) {
                    // FragmentManagerClass.aMine.clear();
                    pagerAdapter.removeView(vpager, f_mine);
                    isMineAdded = false;
                }

            // UPDATE THE ADAPTER
            pagerAdapter.notifyDataSetChanged();
        }

    }

    public void destroy() {

        f_explore = null;
        f_follow = null;
        f_mine = null;
        ImageLoader.getInstance().getMemoryCache().clear();

    }

    public void hideTab() {

        pagerTab.setVisibility(View.GONE);
    }

    public void showTab() {

        pagerTab.setVisibility(View.VISIBLE);
    }

}
